package 美团;

public class _859_亲密字符串 {
    public boolean buddyStrings(String A, String B) {
        if (A == null || B == null) return A == B;
        if (A.length() != B.length()) return false;
        char[] a = A.toCharArray();
        char[] b = B.toCharArray();
        Character one = null;
        Character two = null;
        String str1 = null;
        String str2 = null;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                if (two == null) {
                    if (one == null) {
                      one = a[i];
                      str1 = a[i] + "" + b[i];
                    } else {
                        two = b[i];
                        str2 = b[i] + "" + a[i];
                    }
                } else {
                    return false;
                }
            }
        }
        if (one == null && two == null) return A.equals(B);
        if (one == null || two == null) return false;
        return str1.equals(str2);
    }
}
